<html><head><title>DelayedTask.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>DelayedTask.js</h1><pre class="highlighted"><code><i>/**
 * @class Ext.util.DelayedTask
 * Provides a convenient method of performing setTimeout where a <b>new</b>
 * timeout cancels the old timeout. An example would be performing validation on a keypress.
 * You can use <b>this</b> class to buffer
 * the keypress events <b>for</b> a certain number of milliseconds, and perform only <b>if</b> they stop
 * <b>for</b> that amount of time.
 * @constructor The parameters to <b>this</b> constructor serve as defaults and are not required.
 * @param {Function} fn (optional) The <b>default</b> function to timeout
 * @param {Object} scope (optional) The <b>default</b> scope of that timeout
 * @param {Array} args (optional) The <b>default</b> Array of arguments
 */</i>
Ext.util.DelayedTask = <b>function</b>(fn, scope, args){
    <b>var</b> id = null, d, t;

    <b>var</b> call = <b>function</b>(){
        <b>var</b> now = <b>new</b> Date().getTime();
        <b>if</b>(now - t &gt;= d){
            clearInterval(id);
            id = null;
            fn.apply(scope, args || []);
        }
    };
    <i>/**
     * Cancels any pending timeout and queues a <b>new</b> one
     * @param {Number} delay The milliseconds to delay
     * @param {Function} newFn (optional) Overrides <b>function</b> passed to constructor
     * @param {Object} newScope (optional) Overrides scope passed to constructor
     * @param {Array} newArgs (optional) Overrides args passed to constructor
     */</i>
    <b>this</b>.delay = <b>function</b>(delay, newFn, newScope, newArgs){
        <b>if</b>(id &amp;&amp; delay != d){
            <b>this</b>.cancel();
        }
        d = delay;
        t = <b>new</b> Date().getTime();
        fn = newFn || fn;
        scope = newScope || scope;
        args = newArgs || args;
        <b>if</b>(!id){
            id = setInterval(call, d);
        }
    };

    <i>/**
     * Cancel the last queued timeout
     */</i>
    <b>this</b>.cancel = <b>function</b>(){
        <b>if</b>(id){
            clearInterval(id);
            id = null;
        }
    };
};</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright &copy; 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
    </body></html>